<?php
/** .-------------------------------------------------------------------
* |    Author: 熊伟洋 <chelious@foxmail.com>
* |    WeChat: hello_McGrady
* |    	   QQ: 434493420
* |     Motto: Hungry & Humble
* |---------------------------------------------------------------------
* |    Copyright (c) 2012-2020, www.chelious.com. All Rights Reserved.
* '-------------------------------------------------------------------*/

namespace app\home\controller;

class Lists extends Common {
    //动作
    public function index(){
        //修改title为列表页
        $conf = ['title' => 'CheliOus教学博客-列表页'];
        View::with('conf', $conf);

        $tid = Q('get.tid');
        $cid = Q('get.cid');

        //标签
        if($tid){
            //头部数据
            $HeadData = [
                'type' => '标签',
                'name' => Db::table('tag')->where('tid',$tid)->pluck('tname'),
                'total' => Db::table('article_tag')
                            ->join('article','article_tag.article_aid','=','article.aid')
                            ->where('article_tag.tag_tid',$tid)
                            ->where('is_recycle',0)
                            ->count(),
            ];
//            p($HeadData);

            //文章数据
            $ArticleData = Db::table('article')
                ->join('category','article.category_cid','=','category.cid')
                ->join('article_tag','article_tag.article_aid','=','article.aid')
                ->where('tag_tid',$tid)
                ->where('is_recycle',0)
                ->orderBy('sendtime','desc')
                ->paginate(6);

            $page = $ArticleData->links();
            $ArticleData = $ArticleData->toArray();
            foreach ($ArticleData as $k => $v){
                $ArticleData[$k]['tag'] = Db::table('article_tag')
                    ->join('tag','article_tag.tag_tid','=','tag.tid')
                    ->where('article_aid',$v['aid'])
                    ->field(['tid','tname'])
                    ->get();
            }
//            p($ArticleData);
        }

        //分类
        if($cid){
            //递归找自己 获取自己的儿子
            $cateModel = new \system\model\Category();
            $cids = $cateModel->getSon(Db::table('category')->get(), $cid);
            //追加自己
            $cids[] = $cid;
//            p($cids);

            //头部数据
            $HeadData = [
                'type' => '分类',
                'name' => Db::table('category')->where('cid',$cid)->pluck('cname'),
                'total' => Db::table('article')
                            ->whereIn('category_cid',$cids)
                            ->where('is_recycle',0)
                            ->count(),
            ];
//            p($HeadData);
            //处理文章数据
            $ArticleData = Db::table('article')
                ->join('category','article.category_cid','=','category.cid')
                ->whereIn('category_cid',$cids)
                ->where('is_recycle',0)
                ->orderBy('sendtime','desc')
                ->paginate(6);
            $page = $ArticleData->links();
            $ArticleData = $ArticleData->toArray();
            //加上文章对应的标签
            foreach ($ArticleData as $k => $v){
                $ArticleData[$k]['tag'] = Db::table('tag')
                    ->join('article_tag','article_tag.tag_tid','=','tag.tid')
                    ->where('article_tag.article_aid',$v['aid'])
                    ->field(['tid','tname'])
                    ->get();
            }

//            p($ArticleData);
        }

        View::with('HeadData',$HeadData);
        View::with(['ArticleData'=>$ArticleData,'page'=>$page]);

        return view();
    }
}
